package com.yuejian.neusoft.yuejian_24.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.adapter.SearchAdapter;
import com.yuejian.neusoft.yuejian_24.javaBean.Search;
import com.yuejian.neusoft.yuejian_24.listener.ListListener;
import com.yuejian.neusoft.yuejian_24.model.SearchModel;

import java.util.List;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener,View.OnKeyListener {
    private EditText search;
    private ImageView back;
    private TextView search_bt;
    private RecyclerView recyclerView;
    private Context context;
    ListListener<Search> searchListListener = new ListListener<Search>() {
        @Override
        public void onResponse(List<Search> t) {
            if (t.size()==0){
                Toast.makeText(SearchActivity.this,"没有你想搜索的东西！", Toast.LENGTH_SHORT).show();
            }else {
                SearchAdapter searchAdapter = new SearchAdapter(SearchActivity.this);
                searchAdapter.setList(t);
                recyclerView.setAdapter(searchAdapter);
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initEvent();
    }

    private void initEvent() {
        back.setOnClickListener(this);
        search.setOnKeyListener(this);
        search_bt.setOnClickListener(this);
    }


    private void initView() {
        search = (EditText)findViewById(R.id.search_et);
        back = (ImageView)findViewById(R.id.search_back);
        search_bt = (TextView)findViewById(R.id.search_bt);
        recyclerView = (RecyclerView)findViewById(R.id.rv_search);
        recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.search_back:
                finish();
                break;
            case R.id.search_bt:
                SearchModel searchModel = new SearchModel();
                searchModel.search(search.getText().toString(),searchListListener);
                break;
        }

    }
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        return false;
    }
}
